package com.pro.service.imple;

import com.google.gson.Gson;
import com.pro.mapper.JsCouponsMapper;
import com.pro.pojo.JsCourseRecord;
import com.pro.pojo.JsUserCoupon;
import com.pro.pojo.JsUserCouponUsed;
import com.pro.service.JsCouponsService;
import com.pro.utils.RedisCache;
import jdk.nashorn.internal.ir.GetSplitState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JsCouponsServiceImple implements JsCouponsService {
    @Autowired
    private JsCouponsMapper mapper;

    @Autowired
    private RedisCache redisCache;

    private Gson gson = new Gson();

    public List<JsUserCoupon> selectCoupons(String couponName, String online, String grant, String startDate, String endDate,Integer limit, Integer page){
        page = (page-1)*limit;
        List<JsUserCoupon> jsUserCoupon = mapper.selectCoupons(couponName, online, grant,startDate,endDate,limit,page);
        redisCache.setCacheObject("CouponsSelectCoupons",gson.toJson(jsUserCoupon));
        return jsUserCoupon;
    };

    public Integer countCoupons(String couponName, String online, String grant,String startDate,String endDate){
        Integer integer = mapper.countCoupons(couponName, online, grant,startDate,endDate);
        redisCache.setCacheObject("CouponsCountCoupons",integer);
        return integer;
    };

    //通过id查看优惠券详情
    public JsUserCoupon selectCouponById(Integer couponId){
        JsUserCoupon jsUserCoupon = mapper.selectCouponById(couponId);
        return jsUserCoupon;
    };

    //删除优惠券
    public Boolean deleteCoupon(Integer couponId){
        Boolean aBoolean = mapper.deleteCoupon(couponId);
        return aBoolean;
    };
    //修改优惠券状态
    public Boolean updateCoupon(Integer couponId,Integer online,Integer updateId,String updateDate){
        Boolean aBoolean = mapper.updateCoupon(couponId, online, updateId, updateDate);
        return aBoolean;
    };
    //添加优惠券
    public Boolean addCoupons(JsUserCoupon jsUserCoupon){
        Boolean aBoolean = mapper.addCoupns(jsUserCoupon);
        return aBoolean;
    };
    //查询用户优惠券
    public List<JsUserCoupon> selectUserCoupons(String account,String nickname,String endDate,Integer limit,Integer page){
        page = (page-1)*limit;
        List<JsUserCoupon> jsUserCoupons = mapper.selectUserCoupons(account, nickname, endDate,limit,page);
        redisCache.setCacheObject("CouponsSelectUserCoupons",gson.toJson(jsUserCoupons));
        return jsUserCoupons;
    };
    public Integer countUserCoupons(String account,String nickname,String endDate){
        Integer integer = mapper.countUserCoupons(account, nickname, endDate);
        redisCache.setCacheObject("CouponsCountUserCoupons",integer);
        return integer;
    };
    //查看所有优惠券
    public List<JsUserCoupon> selectGiveCoupons(String couponName,Integer limit,Integer page){
        List<JsUserCoupon> jsUserCoupons = mapper.selectGiveCoupons(couponName,limit,page);
        return jsUserCoupons;
    };
    public Integer countGiveCoupons(String couponName){
        Integer integer = mapper.countGiveCoupons(couponName);
        return integer;
    };

    //为所有前台用户添加优惠券
    public Boolean addBeforeCoupons(List<JsUserCouponUsed> jsUserCouponUsed){
        Boolean aBoolean = mapper.addBeforeCoupons(jsUserCouponUsed);
        return aBoolean;
    };
}
